package fr.upmc.contact;

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import fr.upmc.contact.model.Contact;

public class JSonnerie extends ListActivity {
	
	/**
	 * List of contacts
	 */
	private ContactAdapter adapter;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        
    /* Initialize the Adapter list with a Contact array */
    adapter = new ContactAdapter(this,R.layout.cell_list,new ArrayList<Contact>());
    getListView().setAdapter(adapter);
    
    /* Before loading data from database, check the life cycle of creating Activity :
     * If the bundle was reloading (by orientation), required to reload data */
    ArrayList<Contact> list = null;
    if (savedInstanceState != null){
    	list = savedInstanceState.getParcelableArrayList("contacts");
    }
	if (list == null){
		//initializeContacts(adapter);
		new ContactRequest(adapter).execute();
	}
    }
    
    @Override
	protected void onSaveInstanceState(Bundle outState) {
		/* Save data application by Parceable DATA */
    	outState.putParcelableArrayList("contacts", (ArrayList<Contact>)adapter.getModel());
    	super.onSaveInstanceState(outState);
	}
    
    @Override
    public void onRestoreInstanceState(Bundle inState) {
    	  /* Reload data application from Parceable DATA */
    	  Log.i("TPListView","Loading data instance");
          super.onRestoreInstanceState(inState);
          ArrayList<Contact> list = inState.getParcelableArrayList("contacts");
          adapter.clear();
          adapter.addAll(list);
    }

}